package com.easylinkin.linkappapi.openapi.service.impl;

import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easylinkin.linkappapi.application.service.ApplicationService;
import com.easylinkin.linkappapi.device.mapper.DeviceMapper;
import com.easylinkin.linkappapi.device.service.DeviceService;
import com.easylinkin.linkappapi.deviceunit.mapper.DeviceUnitMapper;
import com.easylinkin.linkappapi.openapi.dto.TokenDTO;
import com.easylinkin.linkappapi.openapi.util.HttpClientUtil;
import com.easylinkin.linkappapi.openapi.util.LinkThingsUtil;
import com.easylinkin.linkappapi.security.service.LinkappUserService;
import com.easylinkin.linkappapi.tenant.entity.LinkappTenant;
import com.easylinkin.linkappapi.tenant.sevice.LinkappTenantService;
import io.netty.util.internal.StringUtil;

/**
 * <p></p>
 *
 * @author TongJie
 * @since 2020/10/12 15:08
 */

@Service
public class OpenApiEasyvService {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(OpenApiEasyvService.class);
    
    @Resource
    private DeviceService deviceService;
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private DeviceUnitMapper deviceUnitMapper;
    
    @Value("${linkthing.clientId}")
    private String clientId;
    @Value("${linkthing.clientSecret}")
    private String clientSecret;
    @Value("${linkthing.grantType}")
    private String grantType;
    @Value("${linkthing.scope}")
    private String scope;
    @Value("${linkthing.tenantId}")
    private String tenantId;
    @Value("${linkthing.url.auth_url}")
    private String auth_url;
    @Value("${linkthing.url.api_url}")
    public String api_url;
    @Value("${linkthing.url.token}")
    private String token;
    @Value("${linkthing.url.get_device_unit_with_device_model}")
    private String get_device_unit_with_device_model;
    @Value("${linkthing.url.device_type_list}")
    private String device_type_list;
    @Value("${linkthing.appSecret}")
    public String apiSecret;
    @Value("${linkthing.url.find_active_app_list}")
    public String find_active_app_list;
    @Value("${linkthing.keyId}")
    public String apiKey;

    @Value("${linkthing.url.get_device_unit_by_project}")
    private String get_device_unit_by_project;

    @Resource
    private LinkappTenantService tenantService;

    @Resource
    private ApplicationService applicationService;
    @Resource
    private LinkappUserService linkappUserService;
    
    private String getPath(String path) {
      if (api_url.endsWith("/") || path.startsWith("/")) {
        return api_url + path;
      }
      return api_url + "/" + path; 
    }

    /**
     * 获取Token
     */
    private TokenDTO getToken() throws UnsupportedEncodingException {
        String url = String.format("%s%s?client_id=%s&client_secret=%s&grant_type=%s&scope=%s", auth_url, token,
            clientId, clientSecret, grantType, scope);
        HttpPost httpPost = new HttpPost(url);
        StringEntity se = new StringEntity(StringUtil.EMPTY_STRING);
        se.setContentType("text/json");
        se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(se);
        LOGGER.info(url);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info("getToken:" + str);
        TokenDTO tokenDTO = JSONObject.parseObject(str, TokenDTO.class);
        return tokenDTO;
    }
    
    /**
     * 初始化HTTP头
     */
    private void initHeader(TokenDTO tokenDTO, HttpRequestBase httpPost, String apikey, String apiSecret, String tenantId) {
        LinkThingsUtil u = new LinkThingsUtil(apiKey, apiSecret);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("appId", clientId);
        httpPost.addHeader("appKey", clientId);
        httpPost.addHeader("accessToken", tokenDTO.getAccess_token());
        httpPost.addHeader("timestamp", u.getTimestamp());
        LOGGER.info("tenantId: " + tenantId);
        httpPost.addHeader("tenantId", tenantId);
        LOGGER.info("initHeader:" + JSONObject.toJSONString(tokenDTO));
    }


    /**
     * 查询3d可视化数据
     * @param token
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dData(String token) throws UnsupportedEncodingException {
        TokenDTO tokenDTO = getToken();
        String path = "/api/ht3d/v1/get/" + token;
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
    /**
     * 更新3d可视化数据
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject update3dData(JSONObject data) throws UnsupportedEncodingException {
        TokenDTO tokenDTO = getToken();
        String path = "/api/ht3d/v1/update";
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
        httpPost.setEntity(se);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
    /**
     * 通过token查询3d可视化数据
     * @param token
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dInstances(String token) throws UnsupportedEncodingException {
        TokenDTO tokenDTO = getToken();
        String path = "/api/ht3d/v1/getInstances/" + token;
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }

    /**
     * 查询项目下的所有3d可视化
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dInstancesByProject() throws UnsupportedEncodingException {
        LinkappTenant linkappTenant = tenantService.currentTenant();
        Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
        TokenDTO tokenDTO = getToken();
        String path = "/api/ht3d/v1/getInstancesByProject/" + linkappTenant.getProjectId();
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }

  /**
   * 查询项目下的所有2d可视化
   * @return
   * @throws UnsupportedEncodingException
   */
  public JSONObject getDatavInstancesByProject(JSONObject data) throws UnsupportedEncodingException {
    LinkappTenant linkappTenant = tenantService.currentTenant();
    Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
    TokenDTO tokenDTO = getToken();
    String path = "/api/Datav/v1/getInstancesByProject/" + linkappTenant.getProjectId();
    HttpPost httpPost = new HttpPost(getPath(path));
    initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
    StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
    httpPost.setEntity(se);
    String str = HttpClientUtil.doPost(httpPost);
    LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
    LOGGER.info(path + " -> response: " + str);
    if (str == null) {
      return null;
    }
    JSONObject parseObject = JSON.parseObject(str);
    return parseObject;
  }
    
    /**
     * 分页查询3d模型列表
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dModelList(JSONObject query) throws UnsupportedEncodingException {
        LinkappTenant linkappTenant = tenantService.currentTenant();
        Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
        TokenDTO tokenDTO = getToken();
        String path = "api/ht3d/model/v1/listPage/" + linkappTenant.getProjectId();
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        StringEntity se = new StringEntity(query.toJSONString(), ContentType.APPLICATION_JSON);
        se.setContentType("text/json");
        se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(se);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
    
    /**
     * 通过id查询3d模型
     * @param id
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dModel(String id) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/model/v1/" + linkappTenant.getProjectId() + "/" + id;
      HttpGet httpPost = new HttpGet(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      String str = HttpClientUtil.doGet(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 添加3d模型
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject add3dModel(JSONObject data) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/model/v1/" + linkappTenant.getProjectId();
      HttpPut httpPost = new HttpPut(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
      se.setContentType("text/json");
      se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
      httpPost.setEntity(se);
      String str = HttpClientUtil.doPut(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 更新3d模型    
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject update3dModel(JSONObject data) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/model/v1/update/" + linkappTenant.getProjectId();
      HttpPost httpPost = new HttpPost(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
      se.setContentType("text/json");
      se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
      httpPost.setEntity(se);
      String str = HttpClientUtil.doPost(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 删除3d模型
     * @param id
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject delete3dModel(String id) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/model/v1/" + linkappTenant.getProjectId() + "/" + id;
      HttpDelete httpPost = new HttpDelete(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      String str = HttpClientUtil.doDelete(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 查询公共模型
     * @param query
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject publicModels(JSONObject query) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "/api/ht3d/model/v1/publicModels?projectId=" + linkappTenant.getProjectId();
      HttpPost httpPost = new HttpPost(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      StringEntity se = new StringEntity(query.toJSONString(), ContentType.APPLICATION_JSON);
      se.setContentType("text/json");
      se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
      httpPost.setEntity(se);
      String str = HttpClientUtil.doPost(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
          return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
  }
    
    /**
     * 分页查询3d图片资源
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dImgList(JSONObject query) throws UnsupportedEncodingException {
        LinkappTenant linkappTenant = tenantService.currentTenant();
        Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
        TokenDTO tokenDTO = getToken();
        String path = "api/ht3d/model/v1/img/listPage/" + linkappTenant.getProjectId();
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        StringEntity se = new StringEntity(query.toJSONString(), ContentType.APPLICATION_JSON);
        se.setContentType("text/json");
        se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(se);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
    
    /**
     * 新增3d图片资源
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject add3dImg(JSONObject data) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/model/v1/img/" + linkappTenant.getProjectId();
      HttpPut httpPost = new HttpPut(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
      se.setContentType("text/json");
      se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
      httpPost.setEntity(se);
      String str = HttpClientUtil.doPut(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 删除3d图片资源
     * @param id
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject delete3dImg(String id) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/model/v1/img/" + linkappTenant.getProjectId() + "/" + id;
      HttpDelete httpPost = new HttpDelete(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      String str = HttpClientUtil.doDelete(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 分页查询3d模型列表
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dModeCategorylList(JSONObject query) throws UnsupportedEncodingException {
        LinkappTenant linkappTenant = tenantService.currentTenant();
        Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
        TokenDTO tokenDTO = getToken();
        String path = "api/ht3d/category/v1/" + linkappTenant.getProjectId() + "/listPage";
        HttpPost httpPost = new HttpPost(getPath(path));
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        StringEntity se = new StringEntity(query.toJSONString(), ContentType.APPLICATION_JSON);
        se.setContentType("text/json");
        se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
        httpPost.setEntity(se);
        String str = HttpClientUtil.doPost(httpPost);
        LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info(path + " -> response: " + str);
        if (str == null) {
            return null;
        }
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
    
    /**
     * 通过id查询3d模型
     * @param id
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get3dModelCategory(String id) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/category/v1/" + linkappTenant.getProjectId() + "/" + id;
      HttpGet httpPost = new HttpGet(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      String str = HttpClientUtil.doGet(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 添加3d模型
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject add3dModelCategory(JSONObject data) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/category/v1/" + linkappTenant.getProjectId();
      HttpPut httpPost = new HttpPut(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
      se.setContentType("text/json");
      se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
      httpPost.setEntity(se);
      String str = HttpClientUtil.doPut(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 更新3d模型    
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject update3dModelCategory(JSONObject data) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/category/v1/" + linkappTenant.getProjectId() + "/update";
      HttpPost httpPost = new HttpPost(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
      se.setContentType("text/json");
      se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
      httpPost.setEntity(se);
      String str = HttpClientUtil.doPost(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    /**
     * 删除3d模型
     * @param id
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject delete3dModelCategory(String id) throws UnsupportedEncodingException {
      LinkappTenant linkappTenant = tenantService.currentTenant();
      Assert.notNull(linkappTenant, "当前租户对象linkappTenant为空");
      TokenDTO tokenDTO = getToken();
      String path = "api/ht3d/category/v1/" + linkappTenant.getProjectId() + "/" + id;
      HttpDelete httpPost = new HttpDelete(getPath(path));
      initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
      String str = HttpClientUtil.doDelete(httpPost);
      LOGGER.info(path + " -> header: "+JSON.toJSONString(httpPost.getAllHeaders()));
      LOGGER.info(path + " -> response: " + str);
      if (str == null) {
        return null;
      }
      JSONObject parseObject = JSON.parseObject(str);
      return parseObject;
    }
    
    
    /**
     * 根据token查询2d可视化
     * @param token
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject get2dData(String token) throws UnsupportedEncodingException {
        TokenDTO tokenDTO = getToken();
        HttpPost httpPost = new HttpPost(api_url + "/api/2d/v1/" + token);
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        String str = HttpClientUtil.doPost(httpPost);
        if (str == null) {
            return null;
        }
        LOGGER.info("api/ht3d/v1/get 的header:"+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info("api/ht3d/v1/get 的url:"+httpPost.getURI());
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
    
    /**
     * 更新2d可视化数据
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject update2dData(JSONObject data) throws UnsupportedEncodingException {
        TokenDTO tokenDTO = getToken();
        HttpPost httpPost = new HttpPost(api_url + "/api/2d/v1/update");
        initHeader(tokenDTO, httpPost, this.apiKey, this.apiSecret, this.tenantId);
        StringEntity se = new StringEntity(data.toJSONString(), ContentType.APPLICATION_JSON);
        httpPost.setEntity(se);
        String str = HttpClientUtil.doPost(httpPost);
        if (str == null) {
            return null;
        }
        LOGGER.info("api/ht3d/v1/update 的header:"+JSON.toJSONString(httpPost.getAllHeaders()));
        LOGGER.info("api/ht3d/v1/update 的url:"+ httpPost.getURI());
        JSONObject parseObject = JSON.parseObject(str);
        return parseObject;
    }
}
